﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM
{
    public partial class Test : System.Web.UI.Page
    {
        string sErr = "";
        const string _singleAns = "Single answers";
        const string _multipleAns = "Multiple answers";
        const string _freeAns = "Free answer";
        const string _orderAns = "Ordering answers";
        const string _dsetName = "dsTestLog";
        protected void Page_Load(object sender, EventArgs e)
        {
            /*  protected void Button1_Click(object sender, EventArgs e)  
                {  
                    string scriptstring = "<script language='javascript'>function f()  {      radconfirm('Client radconfirm: Are you sure?', confirmCallBackFn, 330, 100, null,'Client RadConfirm'); return false; Sys.Application.remove_load(f);  };  Sys.Application.add_load(f); </script>";

                    Page.ClientScript.RegisterStartupScript(this.GetType(), "startTimer", scriptstring);  
                }
             *  function confirmCallBackFn(arg)
                {
                    radalert("<strong>radconfirm</strong> returned the following result: <h3 style='color: #ff0000;'>" + arg + "</h3>", null, null, "Result");
                }
             */
            DataSet dset = new DataSet(_dsetName);
            if (!IsPostBack)
            {
                Session["backlink"] = Request.UrlReferrer.OriginalString;
                if (Request["TestID"] != null)
                    Session["TestID"] = Request["TestID"].ToString();
                else
                {
                    Response.Redirect("TestTemplate.aspx");
                }
                txtTestID.Text = Session["TestID"].ToString();
                //BUS.TestControl ctr = new BUS.TestControl();
                CreateTest(ref dset);
                //dset.Tables["dtQuest"].DefaultView.Sort = "Line";
                //dset.Tables["dtAnswer"].DefaultView.Sort = "Test_Log_Line,Rank";
                Session[_dsetName] = null;
                Session[_dsetName] = dset;
                Session["QuestIndex"] = 0;
                mtq_question_container.Visible = false;
                lbNumofQuest.Text = dset.Tables["dtQuest"].Rows.Count.ToString();
                lbNumOfAns.Text = "0";
            }
            Win_ViewReport.VisibleOnPageLoad = false;
            //btnList.Attributes["onclick"] = "openWin_ViewLookUp();return false";

            //dlAnswer.ItemCreated += new DataListItemEventHandler(dlAnswer_ItemCreated);
        }

        void dlAnswer_ItemCreated(object sender, DataListItemEventArgs e)
        {

        }

        //private string GetLabel(int p)
        //{
        //    switch (p)
        //    {
        //        case 0:
        //            return "a";
        //        case 1:
        //            return "b";
        //        case 2:
        //            return "c";
        //        case 3:
        //            return "a";
        //        case 4:
        //            return "e";
        //        case 5:
        //            return "f";
        //        case 6:
        //            return "g";
        //        default: return "";
        //    }
        //}



        private void SetDataToPage(int index, DataSet dset)
        {
            if (index == 0)
                btnPrev.Enabled = false;
            else
                btnPrev.Enabled = true;
            if (index == dset.Tables["dtQuest"].Rows.Count - 1)
            {
                btnNext.Enabled = false;
            }
            else btnNext.Enabled = true;

            //DTO.Test_LogInfo inf = new DTO.Test_LogInfo(dset.Tables["dtQuest"].Rows[index]);
            lbLine.Text = "Question " + (index + 1) + ":";
            //DataRow[] arrRowQ = dset.Tables["dtQuest"].Select("Line=" + (index + 1));
            DataRow rowQuest = dset.Tables["dtQuest"].Rows[index];
            lbQuest.Text = rowQuest["QuestName"].ToString();
            string questType = rowQuest["QuestType"].ToString();
            lbType.Text = "Question Type : " + questType;
            DataTable dt = dset.Tables["dtAnswer"].Clone();
            DataRow[] rows = dset.Tables["dtAnswer"].Select("Test_Log_No = " + rowQuest["No"].ToString() + " and Test_Log_Line = " + rowQuest["Line"].ToString(), "Rank");
            foreach (DataRow row in rows)
            {
                DataRow newrow = dt.NewRow();
                newrow.ItemArray = row.ItemArray;
                dt.Rows.Add(newrow);
            }
            dlAnswer.DataSource = dt;

            dlAnswer.DataBind();
        }

        private void CreateTest(ref DataSet dset)
        {

            Session[_dsetName] = dset;
            txtTestID.Text = Session["TestID"].ToString();
            Session["TestID"] = null;
            BUS.TestControl testCtr = new BUS.TestControl();
            DTO.TestInfo testInf = testCtr.Get(Convert.ToInt64(txtTestID.Text), ref sErr);
            lbMin.Text = testInf.Duration_Time.ToString("");
            hdMin.Value = lbMin.Text;
            lbPassScore.Value = Math.Round(100 * (testInf.Pass_Score / testInf.Max_Score), 2).ToString();
            lbSec.Text = "00";
            hdSec.Value = "00";
            lbName.Text = testInf.Name;
            lbDescription.Text = testInf.Description;

            BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
            int No = logCtr.GetNoTemp(Convert.ToInt64(txtTestID.Text), "TST");
            txtNo.Text = No.ToString();
            int iToday = BUS.CommonControl.ConvertDateToInt(DateTime.Today);
            if (!dset.Tables.Contains("dtQuest"))
            {

                DataTable dtQuest = new DataTable();
                if (testInf.Random_Question_S || testInf.Random_Question_O)
                    dtQuest = logCtr.GetLogRandByNo(No, ref sErr);
                else
                    dtQuest = logCtr.GetLogByNo(No, ref sErr);
                dtQuest.Columns.Add("IsAnswer", typeof(int));

                dtQuest.TableName = "dtQuest";

                dset.Tables.Add(dtQuest.Copy());

                //DataRelation relat = new DataRelation("reQuest_Ans", new DataColumn[] { dtQuest.Columns["No"], dtQuest.Columns["Line"] }, new DataColumn[] { dtAnswer.Columns["Test_Log_No"], dtAnswer.Columns["Test_Log_Line"] });
                //dset.Relations.Add(relat);
            }
            //lbMin.Text = testInf.Duration_Time.ToString();
            //lbSec.Text = "00";
            DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
            foreach (DataRow row in dset.Tables["dtQuest"].Rows)
            {
                row["Creation_Time"] = iToday;
                row["User_ID"] = user.ID;
            }
            RanDomQuestion(dset, testInf.Random_Answers_S, testInf.Random_Answers_O, No);
            //if (testInf.Random_Question_S || testInf.Random_Question_O)
                dset = RebuildDataSet(dset);
            int[] countAns = new int[dset.Tables["dtQuest"].Rows.Count];
            for (int i = 0; i < countAns.Length; i++)
            {
                countAns[i] = 0;
            }
            Session["CountAns"] = countAns;

        }

        private DataSet RebuildDataSet(DataSet dset)
        {
            DataSet result = dset.Clone();
            int index = 1;
            foreach (DataRow row in dset.Tables["dtQuest"].Rows)
            {
                DataRow newRow = result.Tables["dtQuest"].NewRow();
                newRow.ItemArray = row.ItemArray;
                newRow["Line"] = index;
                DataRow[] arrAnsRow = dset.Tables["dtAnswer"].Select("Test_Log_No = " + row["No"].ToString() + " and Test_Log_Line = " + row["Line"].ToString());
                int indexA = 1;
                foreach (DataRow ansRow in arrAnsRow)
                {
                    DataRow newARow = result.Tables["dtAnswer"].NewRow();
                    newARow.ItemArray = ansRow.ItemArray;
                    newARow["Test_Log_Line"] = index;
                    newARow["Rank"] = indexA;
                    result.Tables["dtAnswer"].Rows.Add(newARow);
                    indexA++;
                }
                result.Tables["dtQuest"].Rows.Add(newRow);
                index++;
            }
            return result;
        }

        private void RanDomQuestion(DataSet dset, bool Random_Answers_S, bool Random_Answers_O, int No)
        {
            if (Random_Answers_S || Random_Answers_O)
            {
                DTO.Test_Log_AnswerInfo tmp = new DTO.Test_Log_AnswerInfo();
                DataTable dtAnswer = tmp.ToDataTable();
                dtAnswer.Columns.Add("AnsName");
                dtAnswer.TableName = "dtAnswer";
                dset.Tables.Add(dtAnswer);
                BUS.AnswerControl ansCtr = new BUS.AnswerControl();
                foreach (DataRow row in dset.Tables["dtQuest"].Rows)
                {
                    int iLine = Convert.ToInt32(row["Line"].ToString());
                    DataTable dt = ansCtr.Get_RandByIDQuestion(Convert.ToInt64(row["Question_ID"].ToString()), ref sErr);
                    foreach (DataRow asnRow in dt.Rows)
                    {
                        tmp = new DTO.Test_Log_AnswerInfo();
                        tmp.Answer_ID = Convert.ToInt64(asnRow["ID"]);
                        tmp.Test_Log_Line = iLine;
                        tmp.Answer_Selected = false;
                        tmp.Test_Log_No = No;
                        tmp.Rank = dt.Rows.IndexOf(row);
                        DataRow tmpRow = tmp.ToDataRow(dset.Tables["dtAnswer"]);
                        tmpRow["AnsName"] = asnRow["Description"];
                        dset.Tables["dtAnswer"].Rows.Add(tmpRow);
                    }
                }
            }
            else
            {
                BUS.Test_Log_AnswerControl ansCtr = new BUS.Test_Log_AnswerControl();
                DataTable dtAnswer = ansCtr.GetLogByNo(No, ref sErr);
                foreach (DataRow asnRow in dtAnswer.Rows)
                {
                    asnRow["Answer_Selected"] = false;
                }
                dtAnswer.TableName = "dtAnswer";
                dset.Tables.Add(dtAnswer.Copy());
            }
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            int index = (int)Session["QuestIndex"];
            if (index != 0)
            {
                SaveSelect(dset, index);
                index--;
                Session["QuestIndex"] = index;
                btnPrev.Enabled = index != 0;
                btnNext.Enabled = index != dset.Tables["dtQuest"].Rows.Count - 1;


                SetDataToPage(index, dset);
                lbExplan.Text = "";
            }

        }

        private void SaveSelect(DataSet dset, int index)
        {
            //DataTable dt = dlAnswer.DataSource as DataTable;
            bool flag = true;
            bool flasans = false;
            foreach (ListViewItem item in dlAnswer.Items)
            {
                CheckBox ckb = (CheckBox)item.FindControl("ckbAnswer");
                RadioButton rb = (RadioButton)item.FindControl("rbAnswer");
                //ckb.Checked = Convert.ToBoolean(row["Answer_Selected"]);
                HiddenField hdNo = (HiddenField)item.FindControl("hdNo");
                TextBox txt = (TextBox)item.FindControl("txtAnswer");
                //hdNo.Value = row["Test_Log_No"].ToString();
                HiddenField hdLine = (HiddenField)item.FindControl("hdLine");
                //hdLine.Value = row["Test_Log_Line"].ToString();
                HiddenField hdAnsID = (HiddenField)item.FindControl("hdAnsID");
                //hdAnsID.Value = row["Answer_ID"].ToString();
                DataRow[] arow = dset.Tables["dtAnswer"].Select("Test_Log_No = " + hdNo.Value + " and Test_Log_Line = " + hdLine.Value + " and Answer_ID = " + hdAnsID.Value);
                DataRow[] rowQ = dset.Tables["dtQuest"].Select("No = " + hdNo.Value + " and Line = " + hdLine.Value);
                string questType = rowQ[0]["QuestType"].ToString();
                //DTO.Test_Log_AnswerInfo inf = new DTO.Test_Log_AnswerInfo(arow[0]);
                BUS.AnswerControl ansCtr = new BUS.AnswerControl();
                DTO.AnswerInfo ansInf = ansCtr.Get(Convert.ToInt64(arow[0]["Answer_ID"].ToString()), ref sErr);

                if (questType == _singleAns)
                {
                    arow[0]["Answer_Selected"] = rb.Checked;
                    flag = flag & rb.Checked == ansInf.IsRight;
                    if (rb.Checked)
                        flasans = true;
                }
                else if (questType == _multipleAns)
                {
                    arow[0]["Answer_Selected"] = ckb.Checked;
                    flag = flag & ckb.Checked == ansInf.IsRight;
                    if (ckb.Checked)
                        flasans = true;
                }
                else if ((questType == _freeAns))
                {
                    arow[0]["Answer_Text"] = txt.Text;
                    flag = flag & txt.Text.ToLower() == ansInf.Text_Answer.ToLower();
                    if (txt.Text != "")
                        flasans = true;
                }
                else if ((questType == _orderAns))
                {
                    arow[0]["Answer_Order"] = txt.Text;
                    flag = flag & txt.Text.ToLower() == ansInf.Order_Answer.ToLower();
                    if (txt.Text != "")
                        flasans = true;
                }

            }
            if (flasans)
            {
                int[] countAns = Session["CountAns"] as int[];
                countAns[index] = 1;
            }
            DataRow[] arrRowQ = dset.Tables["dtQuest"].Select("Line=" + (index + 1));

            arrRowQ[0]["Score"] = flag ? 1 : 0;
            dset.Tables["dtQuest"].AcceptChanges();
            dset.Tables["dtAnswer"].AcceptChanges();

        }
        public string GetValueCheck(object val)
        {
            if (val is Boolean)
            {
                if ((bool)val)
                    return "True";
                else return "False";
            }
            return "False";
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            int index = (int)Session["QuestIndex"];
            if (index != dset.Tables["dtQuest"].Rows.Count - 1)
            {
                SaveSelect(dset, index);
                index++;
                Session["QuestIndex"] = index;
                btnPrev.Enabled = index != 0;
                btnNext.Enabled = index != dset.Tables["dtQuest"].Rows.Count - 1;


                SetDataToPage(index, dset);
                lbExplan.Text = "";
            }


        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            BUS.TestControl testCtr = new BUS.TestControl();
            DTO.TestInfo testInf = testCtr.Get(Convert.ToInt64(txtTestID.Text), ref sErr);
            //lbMin.Text = testInf.Duration_Time.ToString("");
            //lbSec.Text = "00";
            //Timer1.Enabled = true;
            Session["Duration_Time"] = testInf.Duration_Time * 60;
            DataSet dset = Session[_dsetName] as DataSet;
            if (mtq_question_container.Visible == false)
            {

                SetDataToPage(0, dset);
                btnStart.Text = "Get Result";
                mtq_question_container.Visible = true;
                Session["FlagTest"] = 1;
                StartTimer();
            }
            else
            {
                GetResult(dset);
            }
        }

        private void GetResult(DataSet dset)
        {
            StopTimer();

            int index = (int)Session["QuestIndex"];
            SaveSelect(dset, index);

            int no = SaveResult(_dsetName);
            double score = Convert.ToDouble(dset.Tables["dtQuest"].Compute("SUM(Score)", ""));
            lbCorrect.Text = score.ToString("N0");
            double rate = Math.Round(100 * score / dset.Tables["dtQuest"].Rows.Count, 2);
            lbScore.Text = Math.Round(100 * score / dset.Tables["dtQuest"].Rows.Count, 2).ToString();
            if (rate < Convert.ToDouble(lbPassScore.Value))
            {
                lbNotes.Text = ConfigurationSettings.AppSettings["MsgFail"];
                imgMsg.ImageUrl = "images/cry.png";
            }
            else
            {
                lbNotes.Text = ConfigurationSettings.AppSettings["MsgPass"];
                imgMsg.ImageUrl = "images/smile.png";
            }
            int[] Countans = Session["CountAns"] as int[];
            int total = 0;
            for (int i = 0; i < Countans.Length; i++)
            {
                total += Countans[i];
            }
            lbNumOfAns.Text = total.ToString();

            if (no != 0)
            {
                dlAnswer.Enabled = false;
                btnList.Enabled = btnNext.Enabled = btnPrev.Enabled = false;
                hdNo.Value = no.ToString();
                Session["QuestIndex"] = null;
                Session["Duration_Time"] = null;
                Session["CountAns"] = null;
                Session["FlagTest"] = null;
                Session[_dsetName] = null;
                //Response.Redirect("TestTemplate.aspx");
            }
            mtq_quiz_results_bubble.Visible = true;
            btnStart.Visible = false;
        }

        private void StopTimer()
        {
            string scriptstring = "<script language='javascript'>function f()  {     timer.stop(); Sys.Application.remove_load(f);  };  Sys.Application.add_load(f); </script>";

            Page.ClientScript.RegisterStartupScript(this.GetType(), "stopTimer", scriptstring);
        }

        private void StartTimer()
        {
            string scriptstring = "<script language='javascript'>function f()  {     timer.init('" + lbMin.ClientID + "','" + lbSec.ClientID + "'); Sys.Application.remove_load(f);  };  Sys.Application.add_load(f); </script>";

            Page.ClientScript.RegisterStartupScript(this.GetType(), "startTimer", scriptstring);
        }



        private int SaveResult(string _dsetName)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
            long idTest = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["Test_ID"]);
            long idUrs = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["User_ID"]);
            string type = Convert.ToString(dset.Tables["dtQuest"].Rows[0]["Type"]);
            //if (type == "TMP")
            //    sErr = logCtr.Delete(idTest, idUrs, type);
            BUS.Test_Log_AnswerControl ansCtr = new BUS.Test_Log_AnswerControl();
            int No = logCtr.GetMaxNo() + 1;
            foreach (DataRow row in dset.Tables["dtQuest"].Rows)
            {
                DTO.Test_LogInfo logInf = new DTO.Test_LogInfo(row);
                logInf.Display_Time = Convert.ToInt32(hdMin.Value) * 60 + Convert.ToInt32(hdSec.Value);
                DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
                if (user == null)
                    Response.Redirect("Login.aspx?hander=save");
                DataRow[] rows = dset.Tables["dtAnswer"].Select("Test_Log_No = " + logInf.No + " and Test_Log_Line = " + logInf.Line);
                for (int i = rows.Length - 1; i >= 0; i--)
                {
                    DataRow rowA = rows[i];
                    DTO.Test_Log_AnswerInfo aInf = new DTO.Test_Log_AnswerInfo(rowA);
                    aInf.Test_Log_No = No;
                    ansCtr.Add(aInf, ref sErr);
                    dset.Tables["dtAnswer"].Rows.Remove(rowA);
                }
                logInf.User_ID = user.ID;
                logInf.No = No;
                //logInf.Type = "";
                logCtr.Add(logInf, ref sErr);

            }
            if (sErr != "")
                No = 0;
            return No;

        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ShowRadWindow(RadWindowManager2, Win_ViewReport, "./Windows/frmListQ.aspx", new object[] { "" });
        }
        protected void ShowRadWindow(RadWindowManager radManager, RadWindow radWindow, string url, object[] Args)
        {
            RadWindow selectedWindow = radManager.Windows[radManager.Windows.IndexOf(radWindow)];
            selectedWindow.EnableViewState = false;
            selectedWindow.NavigateUrl = ResolveUrl(string.Format(url, Args));
            selectedWindow.VisibleOnPageLoad = true;
        }
        protected void CloseRadWindow(RadWindowManager radManager, RadWindow radWindow)
        {
            RadWindow selectedWindow = radManager.Windows[radManager.Windows.IndexOf(radWindow)];
            selectedWindow.EnableViewState = false;
            selectedWindow.NavigateUrl = "";
            selectedWindow.VisibleOnPageLoad = false;
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            CloseRadWindow(RadWindowManager2, Win_ViewReport);

            if (hdCOMMAND_TYPE.Value != "")
            {
                DataSet dset = Session[_dsetName] as DataSet;
                //for (int i = 0; i < dset.Tables["dtQuest"].Rows.Count; i++)
                //{
                //    if (dset.Tables["dtQuest"].Rows[i]["Line"].ToString() == hdCOMMAND_TYPE.Value)
                //    {
                int i = Convert.ToInt32(hdCOMMAND_TYPE.Value);
                int index = (int)Session["QuestIndex"];
                SaveSelect(dset, index);
                Session["QuestIndex"] = i;

                SetDataToPage(i, dset);
                btnPrev.Enabled = i != 0;
                btnNext.Enabled = i != dset.Tables["dtQuest"].Rows.Count - 1;
                lbExplan.Text = "";
                //break;
                //    }
                //}

            }
            hdCOMMAND_TYPE.Value = "";
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (Session["Duration_Time"] != null)
            {
                int Duration_Time = Convert.ToInt32(Session["Duration_Time"]);
                int min = Duration_Time / 60;
                int second = Duration_Time - min * 60;
                lbMin.Text = min.ToString();
                lbSec.Text = second.ToString("00");
                Duration_Time--;
                Session["Duration_Time"] = Duration_Time;
                if (Duration_Time == 0)
                {
                    btnStart_Click(null, null);
                }
            }
        }



        protected void dlAnswer_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item is ListViewDataItem)
            {
                DataSet dset = Session[_dsetName] as DataSet;
                DataRow row = ((DataRowView)((ListViewDataItem)e.Item).DataItem).Row;
                Literal txtNum = (Literal)e.Item.FindControl("txtNum");
                //txtNum.Text = GetLabel(e.Item.ItemIndex);

                TextBox txt = (TextBox)e.Item.FindControl("txtAnswer");

                HiddenField hdNo = (HiddenField)e.Item.FindControl("hdNo");
                hdNo.Value = row["Test_Log_No"].ToString();
                HiddenField hdLine = (HiddenField)e.Item.FindControl("hdLine");
                hdLine.Value = row["Test_Log_Line"].ToString();
                HiddenField hdAnsID = (HiddenField)e.Item.FindControl("hdAnsID");
                hdAnsID.Value = row["Answer_ID"].ToString();
                CheckBox ckb = (CheckBox)e.Item.FindControl("ckbAnswer");
                RadioButton rb = (RadioButton)e.Item.FindControl("rbAnswer");
                DataRow[] rowQ = dset.Tables["dtQuest"].Select("No = " + hdNo.Value + " and Line = " + hdLine.Value);
                string questType = rowQ[0]["QuestType"].ToString();

                rb.Checked = Convert.ToBoolean(row["Answer_Selected"]);
                ckb.Checked = Convert.ToBoolean(row["Answer_Selected"]);

                string script = "SetUniqueRadioButton('.*dlAnswer.*Quest',this)";
                rb.Attributes.Add("onclick", script);
                if (questType == _singleAns)
                {
                    rb.Attributes.Add("style", "float: left;margin-left: 10px;margin-top: 10px;position: relative;");
                    rb.Visible = true;
                    ckb.Visible = false;
                    txt.Visible = false;
                }
                else if (questType == _multipleAns)
                {
                    ckb.Attributes.Add("style", "float: left;margin-left: 10px;margin-top: 10px;position: relative;");
                    rb.Visible = false;
                    ckb.Visible = true;
                    txt.Visible = false;
                }
                else if (questType == _freeAns)
                {
                    txt.Attributes.Add("style", "float: left;margin-left: 60px;width: 35px;");
                    txt.Height = 35;
                    txt.Width = 800;
                    txt.TextMode = TextBoxMode.MultiLine;
                    txt.Text = row["Answer_Text"].ToString();
                    txt.Visible = true;
                    rb.Visible = false;
                    ckb.Visible = false;
                }
                else if (questType == _orderAns)
                {
                    txt.Attributes.Add("style", "float: left;margin-top: 10px;width: 35px;");
                    txt.MaxLength = 1;
                    txt.Visible = true;
                    rb.Visible = false;
                    ckb.Visible = false;
                    txt.Text = row["Answer_Order"].ToString();
                }


            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            if (Session["backlink"] != null)
            {
                Response.Redirect(Session["backlink"].ToString());
            }
            Response.Redirect("TestTemplate.aspx");
        }

        protected void btnHistory_Click(object sender, EventArgs e)
        {
            Response.Redirect("DetailTest.aspx?TestNo=" + hdNo.Value);
        }
        protected void ckbAnswer_CheckedChanged(object sender, EventArgs arg)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            int index = (int)Session["QuestIndex"];
            dset.Tables["dtQuest"].Rows[index]["IsAnswer"] = 1;
            //((CheckBox)sender).Enabled = false;
        }
        protected void rbAnswer_CheckedChanged(object sender, EventArgs arg)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            int index = (int)Session["QuestIndex"];
            dset.Tables["dtQuest"].Rows[index]["IsAnswer"] = 1;
            //dlAnswer.Enabled = false;
        }
        protected void txtAnswer_TextChanged(object sender, EventArgs arg)
        {
            DataSet dset = Session[_dsetName] as DataSet;
            int index = (int)Session["QuestIndex"];
            dset.Tables["dtQuest"].Rows[index]["IsAnswer"] = 1;
            //((TextBox)sender).Enabled = false;
        }





    }
}